<?php
declare (strict_types=1);

namespace app\admin\controller;


use app\admin\validate\AdminMenusValidate;
use app\admin\validate\BaseValidate;
use app\admin\validate\ConfigValidate;
use app\domain\admin\AdminMenusDomain;
use think\response\Json;

class AdminMenus extends Common
{
    public function retrieve(AdminMenusDomain $domain): Json
    {
        try {
            $result = $domain->retrieve();
            return jsonReturn(0, $domain->message, $result);
        } catch (\Exception $e) {
            return jsonReturn(1, lang($e->getMessage()));
        }
    }

    public function update(AdminMenusDomain $domain): Json
    {
        try {
            $params = request()->param();
            $validate = validate(AdminMenusValidate::class);
            if (!$validate->scene('update')->check($params)) {
                throw new \Exception($validate->getError());
            }

            $result = $domain->update($params['id'], $params['pid'] ?? 0, $params['title'], $params['icon'] ?? '', $params['route'] ?? '', $params['redirect'] ?? '', $params['component_name'], $params['component'] ?? '', $params['sort'] ?? 0, $params['status'] ?? 'enable');
            return jsonReturn($result ? 0 : 1, $domain->message, $result);
        } catch (\Exception $e) {
            return jsonReturn(1, $e->getMessage());
        }
    }

    public function create(AdminMenusDomain $domain): Json
    {
        try {
            $params = request()->param();
            $validate = validate(AdminMenusValidate::class);
            if (!$validate->scene('create')->check($params)) {
                throw new \Exception($validate->getError());
            }

            $result = $domain->create($params['pid'] ?? 0, $params['title'], $params['icon'] ?? '', $params['route'] ?? '', $params['redirect'] ?? '', $params['component_name'], $params['component'] ?? '', $params['sort'] ?? 0, $params['status'] ?? 'enable');
            return jsonReturn($result ? 0 : 1, $domain->message, $result);
        } catch (\Exception $e) {
            return jsonReturn(1, $e->getMessage());
        }
    }

    public function delete(AdminMenusDomain $domain): Json
    {
        try {
            $params = request()->param();
            $validate = validate(BaseValidate::class);
            if (!$validate->scene('delete')->check($params)) {
                throw new \Exception($validate->getError());
            }

            $result = $domain->delete($params['id']);
            return jsonReturn($result ? 0 : 1, $domain->message, $result);
        } catch (\Exception $e) {
            return jsonReturn(false, $e->getMessage());
        }
    }
}
